<?php
/**
 * Extends ApplyForm to support openID
 *
 * @package Extensions
 * @version @@VERSION@@
 * @subpackage @@EXTENSIONNAME@@
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

if (!defined('IN_VANILLA')) exit();
include_once($Configuration['LIBRARY_PATH'] . 'People/People.Control.ApplyForm.php');

/**
 * Extends ApplyForm to support openID
 *
 * @package Extensions
 * @subpackage @@EXTENSIONNAME@@
 */
 class OpenID_ApplyForm extends ApplyForm {
 	var $RegMode = 'Classic';
 	/**
 	 * @var OpenID_User
 	 */
 	var $Applicant;

 	function OpenID_ApplyForm(&$Context, $FormName = '') {
		$this->Name = 'ApplyForm';
		$this->ValidActions = array('ApplyForm', 'Apply', 'ApplyOpenIdForm', 'ApplyWithOpenId');
		$this->Constructor($Context);

		if ($this->IsPostBack) {
			// Set up the page
			global $Banner, $Foot;
			$Banner->Properties['CssClass'] = 'Apply';
			$Foot->CssClass = 'Apply';
			$this->Context->PageTitle = $this->Context->GetDefinition('ApplyForMembership');
			$this->FormName = $FormName;

			$this->Applicant =& $Context->ObjectFactory->NewContextObject($Context, 'User');
			$this->Applicant->GetPropertiesFromForm();
			$this->CallDelegate('Constructor');

			if ($this->PostBackAction == 'Apply') {
				$this->Applicant->GetPropertiesFromForm();
				// check the user is using
				if (!empty($this->Applicant->OpenIdUrls[0])) {
					$OpenIdManager =& new OpenID_OpenIdManager($this->Context);
					$ReturnUrl = GetUrl($this->Context->Configuration,
						$this->Context->SelfUrl, '', '', '', '',
						'PostBackAction=ApplyOpenIdForm');
					// Redirect
					$OpenIdManager->RequestAuth($this->Applicant->OpenIdUrls[0], $ReturnUrl);
					$this->PostBackValidated = 0;
				} else {
					// Create a regulare account
					$um = $this->Context->ObjectFactory->NewContextObject($this->Context, 'UserManager');
					$this->CallDelegate('PreCreateUser');
					$this->PostBackValidated = $um->CreateUser($this->Applicant);
					$this->CallDelegate('PostCreateUser');
				}
			}

			if ($this->PostBackAction == 'ApplyOpenIdForm') {
				$OpenIdManager =& new OpenID_OpenIdManager($this->Context);
				$this->Applicant->Clear();
				if ($OpenIdManager->ProcessAuthResponse($this->Applicant)) {
					$um = $this->Context->ObjectFactory->NewContextObject($this->Context, 'UserManager');
					if (!$um->GetUserIdByOpenId($this->Applicant->OpenIdUrls[0])) {
						$this->RegMode = 'OpenId';
						$_SESSION['OpenID_Valid'] = $this->Applicant->OpenIdUrls[0];
					} else {
						$this->Context->WarningCollector->Add(
							$this->Context->GetDefinition('You are already register with this user.'));
					}
				}
			}

			if ($this->PostBackAction == 'ApplyWithOpenId') {
				if ($_SESSION['OpenID_Valid']) {
					$this->Applicant->OpenIdUrls[0] = $_SESSION['OpenID_Valid'];
					$this->RegMode = 'OpenId';
					$um = $this->Context->ObjectFactory->NewContextObject($this->Context, 'UserManager');
					$this->PostBackValidated = $um->CreateUser($this->Applicant);
					if($this->PostBackValidated) {
						unset($_SESSION['OpenID_Valid']);
					}
				}
			}

			// Sign applicant in if registered
			if ($this->PostBackValidated && $this->Applicant->UserID > 0) {
				if ($this->Context->Configuration['ALLOW_IMMEDIATE_ACCESS']) {
					$this->Context->Authenticator->AssignSessionUserID($this->Applicant->UserID);
					Redirect($this->Context->Configuration['FORWARD_VALIDATED_USER_URL']);
				} else {
					// Do nothing and let the postbackvalidated template be displayed
				}
			}

			$this->CallDelegate('LoadData');
		}
	}

 	function Render_NoPostBack() {
		$this->Applicant->FormatPropertiesForDisplay();
		if ($this->RegMode === 'OpenId') {
			$this->PostBackParams->Add('PostBackAction', 'ApplyWithOpenId');
			$this->PostBackParams->Add('ReadTerms', $this->Applicant->ReadTerms);
			include(OpenID_ThemeFilePath($this->Context->Configuration,
				'openid_people_apply_form_confirm_open_id.php'));
		} else {
			$this->PostBackParams->Add('PostBackAction', 'Apply');
			$this->PostBackParams->Add('ReadTerms', $this->Applicant->ReadTerms);
			$this->CallDelegate('PreNoPostBackRender');
			include(OpenID_ThemeFilePath($this->Context->Configuration, 'openid_people_apply_form_nopostback.php'));
			$this->CallDelegate('PostNoPostBackRender');
		}
	}

	function Render_ValidPostBack() {
		$this->CallDelegate('PreValidPostBackRender');
		include(OpenID_ThemeFilePath($this->Context->Configuration, 'openid_people_apply_form_validpostback.php'));
		$this->CallDelegate('PostValidPostBackRender');
	}
 }